﻿namespace InstanceController.ConfigurationDAL
{
	/// <summary>
	/// This class handles the communications between the View and the data store. 
	/// Generally this is the business layer logic.  The intent is for the View to issues 
	/// commands such as insert, update, or delete types. And to issue data queries to
	/// read from data storage.  
	/// It is expected that the developer will use the Class Diagram tool to create 
	/// any additional datamodels as necessary.
	/// 
	/// The code below is sample. Your code will/should describe your business intents.
	/// </summary>
	public static class ConfigurationDataMethods
	{
		static readonly ConfigurationDataSource ConfigurationDataSource = new ConfigurationDataSource(true);

		/// <summary>
		/// Saves the configuration. Fails if the record already exists.
		/// </summary>
		/// <param name="theDataModel">The data model.</param>
		/// <returns></returns>
		public static bool SaveConfiguration(ConfigurationDataModel theDataModel)
		{
			//Todo: Business Decision: 
			//Determine your PartitionKey plan and add the partition key now. Also you must add 
			//a unique rowKey.
			//You could simply hardcode it or use a web.config variable, you might even call out
			//to some more sophisticated partition key manager of your own design.  This is a 
			//business driven issue.
			//theDataModel.PartitionKey = "InstanceMonitorConfiguration_" + Guid.NewGuid().ToString("N");
			//theDataModel.RowKey = Guid.NewGuid().ToString("N");		//Example: Generating a Guid as the rowKey

			//Todo: Might want to do additional business logic here

			return ConfigurationDataSource.Create(theDataModel);
		}

		/// <summary>
		/// Updates the configuration or creates a new entry if it does not exist.
		/// </summary>
		/// <param name="theDataModel">The data model.</param>
		/// <param name="overWrite">if set to <c>true</c> [over write].</param>
		/// <returns></returns>
		public static bool UpdateConfiguration(ConfigurationDataModel theDataModel, bool overWrite)
		{
			//Todo: Might want to do additional business logic here
			if (theDataModel == null) return false;
			if (overWrite)
			{
				if (!ConfigurationDataSource.UpdateUnconditional(theDataModel))	//if it can't be updated create a new one
					return ConfigurationDataSource.Create(theDataModel);

			}
			else
			{
				if (!ConfigurationDataSource.Update(theDataModel))				//if it can't be updated create a new one
					return ConfigurationDataSource.Create(theDataModel);
			}
			return true;
		}

		/// <summary>
		/// Deletes the configuration. Fails if the record does not exist.
		/// </summary>
		/// <param name="theDataModel">The data model.</param>
		/// <param name="unConditional">if set to <c>true</c> [un conditional].</param>
		/// <returns></returns>
		public static bool DeleteConfiguration(ConfigurationDataModel theDataModel, bool unConditional)
		{
			//Todo: Might want to do additional business logic here

			if (true)	//add your evaluation here to determine when to delete (if ever)
			{
				return unConditional ? ConfigurationDataSource.DeleteUnconditional(theDataModel) : ConfigurationDataSource.Delete(theDataModel);
			}
		}

		/// <summary>
		/// Reads a ConfigurationDataModel row.
		/// </summary>
		/// <param name="partitionKey">The partition key.</param>
		/// <param name="rowKey">The row key.</param>
		/// <returns></returns>
		public static ConfigurationDataModel ReadRow(string partitionKey, string rowKey)
		{
			return ConfigurationDataSource.Read(partitionKey, rowKey);
		}

	}
}
